package com.gg.fmallspringboot.service.impl;

import com.gg.fmallspringboot.mapper.UserMapper;


import com.gg.fmallspringboot.entity.User;
import com.gg.fmallspringboot.model.Result;
import com.gg.fmallspringboot.service.UserService;
import com.gg.fmallspringboot.utils.MD5Util;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wx
 * @date 2021-06-18 19:12:11
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public Result<User> check(String userName,String password) {
        User user = userMapper.queryUser(userName);
        if (user != null) {
            String md5Pwd = MD5Util.md5(password);
            if (md5Pwd.equals(user.getPassword())){
                JwtBuilder jwtBuilder = Jwts.builder();
                String token = jwtBuilder.setSubject(userName)   //主题，就是token中携带的数据
                        .setIssuedAt(new Date())    //设置token的生成时间
                        .setId(user.getUserId() + "")  //设置用户id为token  id
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))   //设置过期时间
                        .signWith(SignatureAlgorithm.HS256, "QIANfeng666")    //设置加密方式和加密密码
                        .compact();
                return new Result(1000,token,user);
            }else{
                return new Result(1001,"密码错误",null);
            }
        }else{
            return new Result(1002,"用户名不存在",null);
        }
    }
}
